package com.framwork.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;

import java.io.File;

public class ImageLoader {

    private ImageLoader() {
        throw new RuntimeException("ImageLoader cannot be initialized!");
    }

    public static void load(ImageView target, String url) {
        load(target.getContext(), url, target);
    }

    public static void loadLocal(ImageView target, String path) {
        Glide.with(target.getContext())
                .load(new File(path))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .into(target);
    }

    public static void load(ImageView target, Bitmap bitmap) {
        Glide.with(target.getContext())
                .load(bitmap)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(false)
                .into(target);
    }

    public static void load(Context context, String url, View view, int defaultResId, int errorResId) {
        if (!(view instanceof ImageView)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(errorResId)
                .skipMemoryCache(false)
                .placeholder(defaultResId)
                .into((ImageView) view);
    }

    public static void load(Context context, String url, View view, int errorResId) {
        if (!(view instanceof ImageView)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(errorResId)
                .skipMemoryCache(false)
                .into((ImageView) view);
    }

    public static void load(Context context, String url, View view) {
        if (!(view instanceof ImageView)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .into((ImageView) view);
    }

    public static void load(Context context, int resId, View view) {
        if (!(view instanceof ImageView)) {
            return;
        }
        Glide.with(context)
                .load(resId)
                .into((ImageView) view);
    }

    public static void loadCircleCrop(Context context, String url, View view, int errorResId) {
        loadCircleCrop(context, url, view, 0, errorResId);
    }

    public static void loadCircleCrop(Context context, String url, View view, int defaultResId, int errorResId) {
        if (!(view instanceof ImageView)) {
            return;
        }
        if (defaultResId <= 0) {
            Glide.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .skipMemoryCache(false)
                    .error(errorResId)
                    .into((ImageView) view);
        } else {
            Glide.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .skipMemoryCache(false)
                    .error(errorResId)
                    .placeholder(defaultResId)
                    .into((ImageView) view);
        }
    }

    public static void loadCenterCrop(Context context, String url, View view, RequestListener listener) {
        if (!(view instanceof ImageView)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .listener(listener)
                .into((ImageView) view);
    }

    public static void load(Context context, String url, View view, RequestListener listener) {
        if (!(view instanceof ImageView)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .listener(listener)
                .into((ImageView) view);
    }
}
